﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SpeedTest
{
	public enum CopyMode
	{
		Move,
		Copy
	}

	public partial class MoveCopyForm : Form
	{
		private CopyMode mode = CopyMode.Copy;
		private Data data = null;
		private NodeType itemType = NodeType.Undefined;
		private string itemID = String.Empty;

		public MoveCopyForm(Data data, CopyMode mode, NodeType itemType, string itemID)
		{
			InitializeComponent();
			this.data = data;
			this.mode = mode;
			this.itemType = itemType;
			this.itemID = itemID;

			EnableInput(true);
		}

		public MoveCopyForm()
		{
			InitializeComponent();
		}

		private void MoveCopyForm_Load(object sender, EventArgs e)
		{

			cmboPlanTarget.DisplayMember = "Text";
			cmboPlanTarget.ValueMember = "Value";
			cmboRelTarget.DisplayMember = "Text";
			cmboPlanTarget.ValueMember = "Value";

			DataTable relTable = data.GetActiveReleases();
			FillCombo(relTable, relTable.Columns["ID"].Ordinal, relTable.Columns["Description"].Ordinal, cmboRelTarget);
			if (cmboRelTarget.Items.Count > 0) { cmboRelTarget.SelectedIndex = 0; } //triggers the plans to load
		}

		private void EnableInput(bool enable)
		{
			cmboRelTarget.Enabled = enable;

			if (itemType == NodeType.PlanNode) { cmboPlanTarget.Enabled = false; }
			else { cmboPlanTarget.Enabled = enable; }

			switch (mode)
			{
				case CopyMode.Copy:
					{
						chkIncludeEnvirons.Enabled = enable;
						chkIncludeResults.Enabled = enable;
						chkIncludeTesters.Enabled = enable;
					}
					break;
				case CopyMode.Move:
					{
						chkIncludeEnvirons.Enabled = !enable;
						chkIncludeResults.Enabled = !enable;
						chkIncludeTesters.Enabled = !enable;
					}
					break;
			}
			
		}

		private void FillCombo(DataTable table, int valueIndex, int textIndex, ComboBox combo)
		{
			if (table != null && table.Rows.Count > 0)
			{
				combo.Items.Clear();

				for (int i = 0; i < table.Rows.Count; i++)
				{
					ComboItem ci = new ComboItem();
					ci.Text = table.Rows[i][textIndex].ToString();
					ci.Value = table.Rows[i][valueIndex].ToString();

					combo.Items.Add(ci);
				}
			}
		}

		private void cmboRelTarget_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (itemType != NodeType.PlanNode)
			{
				ComboItem ci = (ComboItem)cmboRelTarget.SelectedItem;
				if (ci != null)
				{
					cmboPlanTarget.Items.Clear();
					DataTable planTable = data.GetPlansByRelease(ci.Value);

					FillCombo(planTable, planTable.Columns["ID"].Ordinal, planTable.Columns["Description"].Ordinal, cmboPlanTarget);
				}
			}
		}

		private void btnMoveCopy_Click(object sender, EventArgs e)
		{
			ComboItem relItem = null;
			ComboItem planItem = null;

			if (cmboRelTarget.SelectedItem != null && (cmboPlanTarget.SelectedItem != null || cmboPlanTarget.Enabled == false))
			{
				relItem = (ComboItem)cmboRelTarget.SelectedItem;
				if (cmboPlanTarget.Enabled) { planItem = (ComboItem)cmboPlanTarget.SelectedItem; }
			}
			else
			{
				MessageBox.Show("You must specify a target Release and Plan, if applicable");
			}

			string relID = relItem.Value;
			string planID = planItem == null ? String.Empty : planItem.Value;

			bool res;

			if (mode == CopyMode.Copy) { res = CopyItem(relID, planID); }
			else { res = MoveItem(relID, planID); }

			if (res) { MessageBox.Show("Item copied or moved successfully."); }
			else { MessageBox.Show("Move or copy failed. Please contact the administrator"); }

			this.Close();
		}

		private bool MoveItem(string targetRelease, string targetPlan)
		{
			switch (itemType)
			{
				case NodeType.PlanNode:
					return data.MovePlanByID(itemID, targetRelease);
				case NodeType.SuiteNode:
                    return data.MoveCaseSuiteByID(itemID, targetRelease, targetPlan);
				case NodeType.CaseNode:
                    return data.MoveCaseSuiteByID(itemID, targetRelease, targetPlan);
			}

			return false;
		}

		private bool CopyItem(string targetRelease, string targetPlan)
		{
			switch (itemType)
			{
				case NodeType.PlanNode:
					return data.CopyPlanByID(itemID, targetRelease, chkIncludeResults.Checked, chkIncludeTesters.Checked, 
						chkIncludeEnvirons.Checked);
				case NodeType.SuiteNode:
                    return data.CopyCaseSuiteByID(itemID, targetRelease, targetPlan, chkIncludeResults.Checked,
                        chkIncludeTesters.Checked, chkIncludeEnvirons.Checked);
				case NodeType.CaseNode:
                    return data.CopyCaseSuiteByID(itemID, targetRelease, targetPlan, chkIncludeResults.Checked,
                        chkIncludeTesters.Checked, chkIncludeEnvirons.Checked);
			}

			return false;
		}


	}
}
